<?php
namespace app\common\model\newmodel;

use think\Model;

class AccessorySku extends Model
{
    protected $table = 'accessory_sku';
    protected $pk = 'id';

    /**
     * 配件规格添加
     */
    public static function valueAdd($data)
    {
        if(empty($data['accessory_id'] || empty($data['value']))){
            return ['code'=>false,'msg'=>'参数不完整'];
        }
        try {
            $accessorySku = new self();
            $accessorySku->accessory_id = $data['accessory_id'];
            $accessorySku->value =$data['value'];
            $accessorySku->update_time=time();
            $accessorySku->create_time=time();
            $accessorySku->save();
            return ['code' => true, 'msg' => '配件规格成功'];
        } catch (\Exception $e) {
            return ['code' => false, 'msg' => '配件规格失败：' . $e->getMessage()];
        }
    }
    /**
     * 配件规格编辑
     */
    public static function valueEdit($data)
    {
        if (empty($data['id']) || empty($data['value'])) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }

        $accessorySku = self::get($data['id']);
        if (!$accessorySku) {
            return ['code' => 0, 'msg' => '配件规格不存在'];
        }

        try {
            $accessorySku->value = $data['value'];
            $accessorySku->update_time=time();

            $accessorySku->save();
            
            return ['code' => true, 'msg' => '配件规格成功'];
        } catch (\Exception $e) {
            return ['code' => false, 'msg' => '配件规格失败：' . $e->getMessage()];
        }
    }
    /**
     * 配件规格删除
     */
    public static function valueDel($id='')
    {
        if (empty($id)) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }

        $accessorySku = self::get($id);
        if (!$accessorySku) {
            return ['code' => 0, 'msg' => '配件规格不存在'];
        }

        try {
            $accessorySku->delete();
            return ['code' => 1, 'msg' => '配件规格删除成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '配件规格删除失败：' . $e->getMessage()];
        }
    }
}